using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using GoodStuff;

namespace GoodStuff
{
    public class GoodObjectBuilder
    {
        XmlTextReader _reader;

        protected void ParseElement()
        {
            XmlNodeType currentNodeType = _reader.NodeType;
            string currentNodeName = _reader.Name;
        }

        public void ReadFile( string path )
        {
            XmlTextReader r = new XmlTextReader(path);

            Type goodVariableType = Type.GetType("GoodStuff.GoodVariable`1");
            Type goodNumericVariableType = Type.GetType("GoodStuff.GoodNumericVariable`1");
            
            while (r.Read())
            {
                string s;
                int j;

                if ((r.NodeType == XmlNodeType.Element) && (r.Name == "GoodObject"))
                {
                }
                if((r.NodeType == XmlNodeType.Element) && (r.Name == "Property"))
                {
                    string key = r.GetAttribute("Key");

                    switch(r.GetAttribute("Type"))
                    {
                        case "instanceName":
                            break;

                        default:
                            string typeName = "System." + r.GetAttribute("Type");
                            Type[] gvType = new Type[1] {Type.GetType(typeName)};
                            Type t;

                            if(gvType[0].GetInterface("IConvertible") == null)
                                t = goodVariableType.MakeGenericType(gvType);
                            else
                                t = goodNumericVariableType.MakeGenericType(gvType);
                           
                            ConstructorInfo ci = t.GetConstructor(gvType);
                            object[] initialValue = new object[1] { r.ReadElementContentAs(gvType[0], null) };
                            IGoodVariable c = (IGoodVariable)ci.Invoke(initialValue);
                            PropertyInfo pi = t.GetProperty("blort");
                            MethodInfo mi = pi.GetSetMethod();
          //                  mi.Invoke(c, params);
                            break;
                    }
                }

                Trace.WriteLine(r.NodeType.ToString());
                for( int i = 0; i < r.AttributeCount; ++i )
                {
                    string attr = r.GetAttribute(i);
                }
                
            }
        }
    }
}
